// pages/address-add/index.js
const WXAPI = require('apifm-wxapi')
const CONFIG = require('../../config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    province:[],
    index:0,
    city:[],//市的列表数组
    cindex:0, //市的选择id
    proid:"",
    cityid:0,
    area:[],
    aindex:0,
    areaid:0,
    username:'',
    phone:'',
    areainput:'',
    edititem:null
  },
  citychange(e){
    var cid=this.data.city[e.detail.value].id;
      this.setData({
        cindex:e.detail.value,
        cityid:cid
      })
      this.loadArea();
  },
  async deladdress(){ //删除地址接口
   var res=await WXAPI.deleteAddress(wx.getStorageSync('token'),this.data.edititem.id)
   console.log(res)
   if(res.code==0){
     wx.navigateBack()
   }
  },
  async address(e){//添加地址
    var postdata={
      linkMan:this.data.username,
      address:this.data.areainput,
      mobile:this.data.phone,
      token:wx.getStorageSync('token'),
      cityId:this.data.cityid,
      provinceId:this.data.proid,
      districtId:this.data.areaid,
      isDefault:false, //是否默认地址
      code:322000,
    }
    // 修改需要的id
    if(this.data.edititem){//修改地址
      postdata.id=this.data.edititem.id
      var res=await WXAPI.updateAddress(postdata)
      if(res.code==0){
        wx.navigateTo({
          url: '../../pages/select-address/index',
        })
      }

    }else{ //添加地址
      console.log(postdata)
      var res= await WXAPI.addAddress(postdata)
      console.log(res)
      if(res.code==0){
         wx.navigateTo({
           url: '../../pages/to-pay-order/index',
         })
      }
    }


  },
  formchange(e){//输入框赋值
      console.log(e.currentTarget.dataset.name)
     
        this.setData({
          [e.currentTarget.dataset.name]:e.detail.value
        })
  },
  areachange(e){
    var areaid=this.data.area[e.detail.value].id
    this.setData({
      aindex:e.detail.value,
      areaid:areaid
    })
  },
  loadArea(){//加载区
      wx.request({
        url: 'https://api.it120.cc/common/region/v2/child',
        method:"get",
        data:{
          pid:this.data.cityid
        },
        success:res=>{
          console.log(res)
          var area=res.data.data;
          area.unshift({index:"0",name:"请选择"})
          if(this.data.edititem){//修改查找区的下标
            var areaindex=area.findIndex(item=>{
              return item.id==this.data.edititem.districtId
            })
            this.setData({
              districtId:this.data.edititem.districtId,
              aindex:areaindex
            })
          }
        
          this.setData({ 
              area:area
          })
        }
      })
  },
  getaddress(){
    wx.chooseAddress({
      success (res) {
        console.log(res.userName)
        console.log(res.postalCode)
        console.log(res.provinceName)
        console.log(res.cityName)
        console.log(res.countyName)
        console.log(res.detailInfo)
        console.log(res.nationalCode)
        console.log(res.telNumber)
      }
    })
  },
  prochange(e){//省份切換
      console.log(e)
      var proid=this.data.province[e.detail.value].id
      this.setData({
        index:e.detail.value,
        proid:proid
      })
      this.loadCity()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getProvice(){
    wx.request({
      url: 'https://api.it120.cc/common/region/v2/province',
      method:"get",
      success:(res)=>{
        console.log(res)
        var province=res.data.data;
        province.unshift({index:"0",name:"请选择"})
          this.setData({
            province:province
          })
          if(this.data.edititem){ //赋值修改的表单元素
            // 赋值picker和输入框
            // 省的id provinceId 
            console.log(this.data.province)
            var proindex=this.data.province.findIndex(item=>{
                  return item.id==this.data.edititem.provinceId
            })
              console.log(proindex)
            
            this.setData({
              proid:this.data.edititem.provinceId,
              index:proindex,//省的下标
              username:this.data.edititem.linkMan,
              phone:this.data.edititem.mobile,
              areainput:this.data.edititem.address,
            })
            setTimeout(()=>{
              this.loadCity()//加载市
            },0)
            
        }
      }
    
    })
  },
  loadCity(){
        wx.request({
          url: 'https://api.it120.cc/common/region/v2/child',
          method:"get",
          data:{
            pid:this.data.proid
          },
          success:res=>{
            console.log(res)
            var city=res.data.data;
            city.unshift({index:"0",name:"请选择"})
            if(this.data.edititem){ //修改查找市的下标
              var cityindex=city.findIndex(item=>{
                return item.id==this.data.edititem.cityId
              })
              this.setData({
                cindex:cityindex,
                cityid:this.data.edititem.cityId,
              })
              setTimeout(()=>{
                this.loadArea()
              },0)
           }
            
            this.setData({
              city:city
            })
         
          }
        })
  },
  onLoad: function (options) {
 
    WXAPI.init(CONFIG.subDomain)
      this.getProvice(); //获取省的数据
    if(options.item){
      this.setData({
        edititem:JSON.parse(options.item)
      })
    }
   
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})